/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewControl;

import entity.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JSpinner.DateEditor;
import javax.swing.SpinnerDateModel;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.Query;
import org.hibernate.Session;
import util.GUIUtils;

/**
 *
 * @author Jeppe
 */
public class ElevLogPanel extends javax.swing.JPanel {

    private Elev currentElev;
    private Session session;
    private ArrayList aftaler;
    private ArrayList logs;
    private ArrayList historier;
    private Laerer currentLaerer;

    /**
     * Creates new form ElevLogPanel
     */
    public ElevLogPanel(Session session) {
        currentElev = new Elev();
        aftaler = new ArrayList();
        logs = new ArrayList();
        historier = new ArrayList();
        this.session = session;
        initComponents();
    }

    public ElevLogPanel(Session session, Laerer laerer) {
        currentElev = new Elev();
        aftaler = new ArrayList();
        logs = new ArrayList();
        historier = new ArrayList();
        this.session = session;
        this.currentLaerer = laerer;
        initComponents();
    }

    protected void update() {
        int index = cbElev.getSelectedIndex();
        if (currentLaerer == null) {
            // Updater elev combobox
            cbElev.removeAllItems();
            cbElev.addItem("Elev");
            String hql = "from Elev where udskrevet=null";
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                Object[] eList = q.list().toArray();
                Arrays.sort(eList);
                for (int i = 0; i < eList.length; i++) {
                    cbElev.addItem(eList[i]);
                }
                session.getTransaction().commit();
            } catch (HibernateException he) {
                System.out.println(he.getLocalizedMessage());
            }
        } else if (currentLaerer != null) {
            ArrayList holdfags = new ArrayList();
            ArrayList elever = new ArrayList();
            ArrayList finallist = new ArrayList();
            holdfags.addAll(currentLaerer.getHoldfags());
            for (int i = 0; i < holdfags.size(); i++) {
                Holdfag hf = (Holdfag) holdfags.get(i);
                elever.addAll(hf.getHold().getElevs());
                for (int j = 0; j < elever.size(); j++) {
                    Elev e = (Elev) elever.get(i);
                    if (!finallist.contains(e)) {
                        finallist.add(e);
                        cbElev.addItem(e);
                    }
                }
            }
        }

        //ryd alle felter
        GUIUtils.clearFields(this);
        cbElev.setSelectedIndex(index);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGemHistorie = new javax.swing.JButton();
        spHistorieDatoTil = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        spHistorieDatoFra = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        btnSletHistorie = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        listHistorie = new javax.swing.JList();
        jScrollPane6 = new javax.swing.JScrollPane();
        txtaHistorie = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        btnSletLog = new javax.swing.JButton();
        spLogDato = new javax.swing.JSpinner();
        btnGemLog = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtaLog = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        listLog = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        btnGemAftale = new javax.swing.JButton();
        spAftaleDato = new javax.swing.JSpinner();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtaAftale = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        listAftaler = new javax.swing.JList();
        btnSletAftale = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbElev = new javax.swing.JComboBox();
        txtAftaleTitle = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnGemNyAftale = new javax.swing.JButton();
        btnGemNyLog = new javax.swing.JButton();
        btnGemNyHistorie = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();

        setLayout(null);

        btnGemHistorie.setText("Gem ændringer");
        btnGemHistorie.setEnabled(false);
        btnGemHistorie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemHistorieActionPerformed(evt);
            }
        });
        add(btnGemHistorie);
        btnGemHistorie.setBounds(80, 620, 110, 23);

        spHistorieDatoTil.setModel(new javax.swing.SpinnerDateModel());
        spHistorieDatoTil.setEditor(new DateEditor(spHistorieDatoTil, "dd-MM-yyyy"));
        add(spHistorieDatoTil);
        spHistorieDatoTil.setBounds(540, 650, 100, 20);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Til");
        add(jLabel5);
        jLabel5.setBounds(510, 650, 30, 20);

        spHistorieDatoFra.setModel(new javax.swing.SpinnerDateModel());
        spHistorieDatoFra.setEditor(new DateEditor(spHistorieDatoFra, "dd-MM-yyyy"));
        add(spHistorieDatoFra);
        spHistorieDatoFra.setBounds(540, 620, 100, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Fra");
        add(jLabel4);
        jLabel4.setBounds(510, 620, 30, 20);

        btnSletHistorie.setText("Slet");
        btnSletHistorie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletHistorieActionPerformed(evt);
            }
        });
        add(btnSletHistorie);
        btnSletHistorie.setBounds(20, 620, 50, 23);

        listHistorie.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listHistorie.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listHistorieMouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(listHistorie);

        add(jScrollPane5);
        jScrollPane5.setBounds(20, 480, 230, 130);

        txtaHistorie.setColumns(20);
        txtaHistorie.setRows(5);
        jScrollPane6.setViewportView(txtaHistorie);

        add(jScrollPane6);
        jScrollPane6.setBounds(280, 480, 510, 130);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Historie");
        add(jLabel3);
        jLabel3.setBounds(20, 460, 50, 14);

        btnSletLog.setText("Slet");
        btnSletLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletLogActionPerformed(evt);
            }
        });
        add(btnSletLog);
        btnSletLog.setBounds(20, 410, 51, 23);

        spLogDato.setModel(new javax.swing.SpinnerDateModel());
        spLogDato.setEditor(new DateEditor(spLogDato, "dd-MM-yyyy"));
        add(spLogDato);
        spLogDato.setBounds(550, 410, 100, 20);

        btnGemLog.setText("Gem ændringer");
        btnGemLog.setEnabled(false);
        btnGemLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemLogActionPerformed(evt);
            }
        });
        add(btnGemLog);
        btnGemLog.setBounds(80, 410, 110, 23);

        txtaLog.setColumns(20);
        txtaLog.setRows(5);
        jScrollPane4.setViewportView(txtaLog);

        add(jScrollPane4);
        jScrollPane4.setBounds(280, 270, 510, 130);

        listLog.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listLog.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listLogMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(listLog);

        add(jScrollPane2);
        jScrollPane2.setBounds(20, 270, 230, 130);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Log");
        add(jLabel2);
        jLabel2.setBounds(20, 250, 34, 14);

        btnGemAftale.setText("Gem ændringer");
        btnGemAftale.setEnabled(false);
        btnGemAftale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemAftaleActionPerformed(evt);
            }
        });
        add(btnGemAftale);
        btnGemAftale.setBounds(80, 200, 110, 23);

        spAftaleDato.setModel(new SpinnerDateModel());
        spAftaleDato.setEditor(new DateEditor(spAftaleDato, "dd-MM-yyyy"));
        add(spAftaleDato);
        spAftaleDato.setBounds(550, 200, 100, 20);

        txtaAftale.setColumns(20);
        txtaAftale.setRows(5);
        jScrollPane3.setViewportView(txtaAftale);

        add(jScrollPane3);
        jScrollPane3.setBounds(280, 90, 510, 100);

        listAftaler.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listAftaler.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listAftalerMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listAftaler);

        add(jScrollPane1);
        jScrollPane1.setBounds(20, 60, 230, 130);

        btnSletAftale.setText("Slet");
        btnSletAftale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletAftaleActionPerformed(evt);
            }
        });
        add(btnSletAftale);
        btnSletAftale.setBounds(20, 200, 50, 23);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Aftaler");
        add(jLabel1);
        jLabel1.setBounds(20, 40, 50, 14);

        cbElev.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elev" }));
        cbElev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbElevActionPerformed(evt);
            }
        });
        add(cbElev);
        cbElev.setBounds(20, 10, 230, 20);
        add(txtAftaleTitle);
        txtAftaleTitle.setBounds(310, 60, 160, 20);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Title");
        add(jLabel6);
        jLabel6.setBounds(280, 60, 30, 20);

        btnGemNyAftale.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGemNyAftale.setText("Gem Ny Aftale");
        btnGemNyAftale.setEnabled(false);
        btnGemNyAftale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemNyAftaleActionPerformed(evt);
            }
        });
        add(btnGemNyAftale);
        btnGemNyAftale.setBounds(669, 200, 120, 23);

        btnGemNyLog.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGemNyLog.setText("Gem Ny Log");
        btnGemNyLog.setEnabled(false);
        btnGemNyLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemNyLogActionPerformed(evt);
            }
        });
        add(btnGemNyLog);
        btnGemNyLog.setBounds(669, 410, 120, 23);

        btnGemNyHistorie.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnGemNyHistorie.setText("Gem Ny Historie");
        btnGemNyHistorie.setEnabled(false);
        btnGemNyHistorie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGemNyHistorieActionPerformed(evt);
            }
        });
        add(btnGemNyHistorie);
        btnGemNyHistorie.setBounds(660, 620, 130, 23);
        add(jSeparator1);
        jSeparator1.setBounds(0, 240, 1240, 10);
        add(jSeparator2);
        jSeparator2.setBounds(0, 450, 1230, 10);
    }// </editor-fold>//GEN-END:initComponents

    private void cbElevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbElevActionPerformed
        aftaler = new ArrayList();
        logs = new ArrayList();
        historier = new ArrayList();

        if (cbElev.getSelectedIndex() > 0) {
            btnGemNyAftale.setEnabled(true);
            btnGemNyLog.setEnabled(true);
            btnGemNyHistorie.setEnabled(true);
            currentElev = (Elev) cbElev.getSelectedItem();
            Object[] aList = currentElev.getElevaftalers().toArray();
            Arrays.sort(aList);
            aftaler.addAll(Arrays.asList(aList));
            listAftaler.setListData(aList);
            Object[] lList = currentElev.getElevlogs().toArray();
            Arrays.sort(lList);
            logs.addAll(Arrays.asList(lList));
            listLog.setListData(lList);
            Object[] hList = currentElev.getElevhistories().toArray();
            Arrays.sort(hList);
            historier.addAll(Arrays.asList(hList));
            listHistorie.setListData(hList);
        } else {
            update();
            btnGemNyAftale.setEnabled(false);
            btnGemNyLog.setEnabled(false);
            btnGemNyHistorie.setEnabled(false);
        }

    }//GEN-LAST:event_cbElevActionPerformed

    private void btnSletAftaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletAftaleActionPerformed
        if (!listAftaler.isSelectionEmpty()) {
            Elevaftaler ea = (Elevaftaler) aftaler.get(listAftaler.getSelectedIndex());
            session.beginTransaction();
            currentElev.getElevaftalers().remove(ea);
            session.update(currentElev);
            aftaler.remove(listAftaler.getSelectedIndex());
            listAftaler.setListData(aftaler.toArray());
            txtaAftale.setText("");
            txtAftaleTitle.setText("");
            session.getTransaction().commit();
        }

    }//GEN-LAST:event_btnSletAftaleActionPerformed

    private void btnGemAftaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemAftaleActionPerformed
        if (txtAftaleTitle.getText().equals(((Elevaftaler) listAftaler.getSelectedValue()).getTitel())) {
            session.beginTransaction();
            Elevaftaler ea = (Elevaftaler) listAftaler.getSelectedValue();
            currentElev.getElevaftalers().remove(ea);
            session.delete(ea);
            session.getTransaction().commit();
            session.flush();

            session.beginTransaction();
            ElevaftalerId eaid = new ElevaftalerId(currentElev.getCpr(), txtaAftale.getText());

            Elevaftaler nyea = new Elevaftaler(eaid, currentElev, txtAftaleTitle.getText(), (Date) spAftaleDato.getValue());
            currentElev.getElevaftalers().add(nyea);
            session.update(currentElev);
            session.getTransaction().commit();
            update();
        } else {
            txtAftaleTitle.setText(((Elevaftaler) listAftaler.getSelectedValue()).getTitel());
        }
    }//GEN-LAST:event_btnGemAftaleActionPerformed

    private void btnSletLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletLogActionPerformed
        if (!listLog.isSelectionEmpty()) {
            Elevlog el = (Elevlog) logs.get(listLog.getSelectedIndex());
            session.beginTransaction();
            currentElev.getElevlogs().remove(el);
            session.update(currentElev);
            logs.remove(listLog.getSelectedIndex());
            listLog.setListData(logs.toArray());
            txtaLog.setText("");
            session.getTransaction().commit();
        }
    }//GEN-LAST:event_btnSletLogActionPerformed

    private void btnGemLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemLogActionPerformed
        currentElev.getElevlogs().remove((Elevlog) logs.get(listLog.getSelectedIndex()));
        session.beginTransaction();
        Elevlog el = (Elevlog) listLog.getSelectedValue();
        session.delete(el);
        session.getTransaction().commit();
        session.flush();

        session.beginTransaction();

        ElevlogId elid = new ElevlogId(currentElev.getCpr(), txtaLog.getText());
        Elevlog nyel = new Elevlog(elid, currentElev, (Date) spLogDato.getValue());

        currentElev.getElevlogs().add(nyel);

        session.update(currentElev);
        session.getTransaction().commit();
        update();
    }//GEN-LAST:event_btnGemLogActionPerformed

    private void listAftalerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listAftalerMouseClicked
        if (!aftaler.isEmpty()) {
            btnGemAftale.setEnabled(true);
            txtAftaleTitle.setText(((Elevaftaler) aftaler.get(listAftaler.getSelectedIndex())).getTitel());
            txtaAftale.setText(((Elevaftaler) aftaler.get(listAftaler.getSelectedIndex())).getId().getAftale());
            spAftaleDato.setValue(((Elevaftaler) aftaler.get(listAftaler.getSelectedIndex())).getDato());
        } else {
            btnGemAftale.setEnabled(false);
        }
    }//GEN-LAST:event_listAftalerMouseClicked

    private void listLogMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listLogMouseClicked
        if (!logs.isEmpty()) {
            btnGemLog.setEnabled(true);
            txtaLog.setText(((Elevlog) logs.get(listLog.getSelectedIndex())).getId().getLog());
            spLogDato.setValue(((Elevlog) logs.get(listLog.getSelectedIndex())).getDato());
        } else {
            btnGemLog.setEnabled(false);
        }
    }//GEN-LAST:event_listLogMouseClicked

    private void listHistorieMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listHistorieMouseClicked
        if (!historier.isEmpty()) {
            btnGemHistorie.setEnabled(true);
            txtaHistorie.setText(((Elevhistorie) historier.get(listHistorie.getSelectedIndex())).getId().getHistorie());
            spHistorieDatoFra.setValue(((Elevhistorie) historier.get(listHistorie.getSelectedIndex())).getDatofra());
            spHistorieDatoTil.setValue(((Elevhistorie) historier.get(listHistorie.getSelectedIndex())).getDatotil());
        } else {
            btnGemHistorie.setEnabled(false);
        }
    }//GEN-LAST:event_listHistorieMouseClicked

    private void btnSletHistorieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletHistorieActionPerformed
        if (!listHistorie.isSelectionEmpty()) {
            Elevhistorie eh = (Elevhistorie) historier.get(listHistorie.getSelectedIndex());
            session.beginTransaction();
            currentElev.getElevhistories().remove(eh);
            session.update(currentElev);
            historier.remove(listHistorie.getSelectedIndex());
            listHistorie.setListData(historier.toArray());
            txtaHistorie.setText("");
            session.getTransaction().commit();
        }
    }//GEN-LAST:event_btnSletHistorieActionPerformed

    private void btnGemHistorieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemHistorieActionPerformed

        currentElev.getElevhistories().remove((Elevhistorie) historier.get(listHistorie.getSelectedIndex()));

        session.beginTransaction();
        Elevhistorie eh = (Elevhistorie) listHistorie.getSelectedValue();

        session.delete(eh);
        session.getTransaction().commit();
        session.flush();

        session.beginTransaction();
        ElevhistorieId ehid = new ElevhistorieId(currentElev.getCpr(), txtaHistorie.getText());
        Elevhistorie nyeh = new Elevhistorie(ehid, currentElev, (Date) spHistorieDatoFra.getValue(), (Date) spHistorieDatoTil.getValue());
        currentElev.getElevhistories().add(nyeh);
        session.update(currentElev);
        session.getTransaction().commit();
        update();

    }//GEN-LAST:event_btnGemHistorieActionPerformed

    private void btnGemNyAftaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemNyAftaleActionPerformed
        try {
            if (txtAftaleTitle.getText().length() > 0 && txtaAftale.getText().length() > 0) {
                session.beginTransaction();
                ElevaftalerId eaid = new ElevaftalerId(currentElev.getCpr(), txtaAftale.getText());
                Elevaftaler ea = new Elevaftaler(eaid, currentElev, txtAftaleTitle.getText(), (Date) spAftaleDato.getValue());
                currentElev.getElevaftalers().add(ea);
                session.update(currentElev);
                session.getTransaction().commit();
                update();
            } else {
                if (txtaAftale.getText().length() == 0) {
                    txtaAftale.setText("Der mangler tekst!!!");
                }
                if (txtAftaleTitle.getText().length() == 0) {
                    txtAftaleTitle.setText("Der mangler tekst!!!");
                }
            }
        } catch (NonUniqueObjectException e) {
            session.merge(currentElev);
            JOptionPane.showMessageDialog(null, "En aftale med den tekst og den dato findes allerede", "Info", 1);
        }
    }//GEN-LAST:event_btnGemNyAftaleActionPerformed

    private void btnGemNyLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemNyLogActionPerformed
        try {
            if (txtaLog.getText().length() > 0) {
                session.beginTransaction();
                ElevlogId elid = new ElevlogId(currentElev.getCpr(), txtaLog.getText());
                Elevlog el = new Elevlog(elid, currentElev, (Date) spLogDato.getValue());
                currentElev.getElevlogs().add(el);
                session.saveOrUpdate(currentElev);
                session.getTransaction().commit();
                update();
            } else {
                if (txtaLog.getText().length() == 0) {
                    txtaLog.setText("Der mangler tekst!!!");
                }
            }
        } catch (NonUniqueObjectException e) {
            session.merge(currentElev);
            JOptionPane.showMessageDialog(null, "En log med den tekst og den dato findes allerede", "Info", 1);
        }
    }//GEN-LAST:event_btnGemNyLogActionPerformed

    private void btnGemNyHistorieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGemNyHistorieActionPerformed
        try {
            if (((Date) spHistorieDatoFra.getValue()).before(((Date) spHistorieDatoTil.getValue()))) {
                if (txtaHistorie.getText().length() > 0) {
                    session.beginTransaction();
                    ElevhistorieId ehid = new ElevhistorieId(currentElev.getCpr(), txtaHistorie.getText());
                    Elevhistorie eh = new Elevhistorie(ehid, currentElev, (Date) spHistorieDatoFra.getValue(), (Date) spHistorieDatoTil.getValue());
                    currentElev.getElevhistories().add(eh);
                    session.update(currentElev);
                    session.getTransaction().commit();
                    update();
                } else {
                    txtaHistorie.setText("Der mangler tekst!!!");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Datoen fra skal være før datoen til", "Info", 1);
            }

        } catch (NonUniqueObjectException e) {
            session.merge(currentElev);
            JOptionPane.showMessageDialog(null, "Historie med den tekst og de datoer findes allerede", "Info", 1);
        }
    }//GEN-LAST:event_btnGemNyHistorieActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGemAftale;
    private javax.swing.JButton btnGemHistorie;
    private javax.swing.JButton btnGemLog;
    private javax.swing.JButton btnGemNyAftale;
    private javax.swing.JButton btnGemNyHistorie;
    private javax.swing.JButton btnGemNyLog;
    private javax.swing.JButton btnSletAftale;
    private javax.swing.JButton btnSletHistorie;
    private javax.swing.JButton btnSletLog;
    private javax.swing.JComboBox cbElev;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JList listAftaler;
    private javax.swing.JList listHistorie;
    private javax.swing.JList listLog;
    private javax.swing.JSpinner spAftaleDato;
    private javax.swing.JSpinner spHistorieDatoFra;
    private javax.swing.JSpinner spHistorieDatoTil;
    private javax.swing.JSpinner spLogDato;
    private javax.swing.JTextField txtAftaleTitle;
    private javax.swing.JTextArea txtaAftale;
    private javax.swing.JTextArea txtaHistorie;
    private javax.swing.JTextArea txtaLog;
    // End of variables declaration//GEN-END:variables
}
